import 'package:draggable_scrollbar/draggable_scrollbar.dart';

import '../../public.dart';

class ChapterPage extends StatefulWidget {
  final String title;
  final String url;
  final String author;
  final String now;
  final String reptileType;
  final String reptileType1;
  ChapterPage(
      {String title,
      String url,
      String author,
      String now,
      String reptileType,
      String reptileType1,
      List<Chapter> chapterList})
      : this.title = title ?? "",
        this.url = url ?? "",
        this.author = author ?? "",
        this.now = now ?? "",
        this.reptileType = reptileType ?? "",
        this.reptileType1 = reptileType1 ?? "";
  @override
  _ChapterPageState createState() => _ChapterPageState();
}

class _ChapterPageState extends State<ChapterPage>
    with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  ScrollController _customController = ScrollController();
//  HideAlways
  @override
  Widget build(BuildContext context) {
    super.build(context);
    // TODO: implement build
    return Container(
      color: Colors.white,
      child: ProviderWidget<ChapterModel>(
          model: ChapterModel(Provider.of<ThemeModel>(context)),
          onModelReady: (model) async => model.initData(
              title: widget.title,
              url: widget.url,
              author: widget.author,
              now: widget.now.toString(),
              reptileType: widget.reptileType,
              reptileType1: widget.reptileType1),
          builder: (context, model, child) {
            return Scaffold(
              appBar: AppBar(
                centerTitle: true,
                title: Text(
                  widget.title.toString() + "-" + widget.author.toString(),
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(30),
                    color: model.themeModel.customThemeData.mainTextColor
                        .withOpacity(0.6),
                  ),
                ),
              ),
              body: () {
                if (model.busy) {
                  return ViewStateBusyWidget();
                } else if (model.error) {
                  return ViewStateWidget(onPressed: model.initData);
                }
                return DraggableScrollbar(
                  controller: _customController,
                  child: ListView.builder(
                    controller: _customController,
                    itemCount: model.chapterList.length,
                    itemBuilder: (context, index) {
                      return _buildChapter(
                          model, model.chapterList[index], index);
                    },
                  ),
                  heightScrollThumb: 48.0,
                  backgroundColor: Color(0xFFd9d9d9),
                  scrollThumbBuilder: (
                    Color backgroundColor,
                    Animation<double> thumbAnimation,
                    Animation<double> labelAnimation,
                    double height, {
                    Text labelText,
                    BoxConstraints labelConstraints,
                  }) {
                    return FadeTransition(
                      opacity: thumbAnimation,
                      child: Container(
                        height: height,
                        width: 20.0,
                        decoration: BoxDecoration(
                            color: backgroundColor,
                            borderRadius:
                                BorderRadius.all(Radius.circular(20))),
                      ),
                    );
                  },
                );
              }(),
            );
          }),
    );
  }

  Widget _buildChapter(ChapterModel model, Chapter chapter, int index) {
    var item = Column(
      children: <Widget>[
        Ink(
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(color: Color(0xFF999999), width: 0.1))),
          child: InkWell(
            onTap: () async {
              String bookId = Uri.encodeComponent(chapter.bookId);
              String catalogId = Uri.encodeComponent(chapter.catalogId);
              String num = Uri.encodeComponent(chapter.num);
              String position = Uri.encodeComponent("0");
              var route = "/reader?"
                  "bookId=$bookId&"
                  "catalogId=$catalogId&"
                  "num=$num&"
                  "position=$position";
              bool isExist = await DatabaseHelper()
                  .bookExist(chapter.bookId, widget.reptileType);
              if (!isExist) {
                DatabaseHelper().insertBook(
                    bookId: chapter.bookId,
                    bookName: widget.title,
                    author: widget.author,
                    description: model.bookInfo.book.description,
                    imgUrl: model.bookInfo.book.imgUrl,
                    baseUrl: model.bookInfo.book.baseUrl,
                    originUrl: model.bookInfo.book.originUrl,
                    url: model.bookInfo.book.url,
                    updateTime: model.bookInfo.book.updateTime,
                    bookType: model.bookInfo.book.bookType,
                    bookStatus: model.bookInfo.book.bookStatus,
                    reptileType: model.bookInfo.book.reptileType,
                    total: model.bookInfo.list.length,
                    now: index,
                    position: 0);
              }

              FlutterStatusbarManager.setFullscreen(true);
              FlutterStatusbarManager.setHidden(true);
              AppConfig.router.navigateTo(context, route,
                  replace: false, transition: TransitionType.inFromBottom);
            },
            child: Row(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(20, 15, 0, 15),
                  child: Text(
                    chapter.name,
                    style: TextStyle(
                      color: model.themeModel.customThemeData.mainTextColor
                          .withOpacity(0.6),
                      fontSize:
                          ScreenUtil().setSp(24, allowFontScalingSelf: true),
                    ),
                  ),
                )
              ],
            ),
          ),
        )
      ],
    );
    return item;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
